<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head lang="en" th:replace="include/common/common :: commonHeader">
    <footer th:include="include/common/common :: layUiResource"></footer>
    <footer th:include="include/common/common :: bootstrapResource"></footer>
    <footer th:include="include/common/common :: blueCSSResource"></footer>

    <script>

        layui.config({
            base: '/staticResource/layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index' , 'laytpl'] , function () {
            var admin = layui.admin
                ,element = layui.element
                ,form = layui.form;

            element.render();

            let entityName = 'user';
            $(document).ready(function () {
                $.fn.blueJs.tools.formButtonItem(entityName);
            });

            form.on('submit(userSave)',function () {
                let formId = $(this).attr("blue-form-id");
                let newPwd = $("#newPwd").val() , pwd = $("#pwd").val() , returnPwd = $("#returnPwd").val();
                let checkPwd = {
                    pwd:pwd
                }
                //验证原密码是否正确
                $.ajax({
                    type:'POST',
                    url:'/a/user/checkPassword',
                    data:checkPwd,
                    async:false,
                    dataType:'json',
                    success:function (data) {
                        $('.pwd').html("");
                        $(".newPwd").html("");
                        if (!data){
                            $('.pwd').html("原密码不正确，请重新输入");
                        }else {
                            if (newPwd != returnPwd){
                                $(".newPwd").html("两次输入密码不一致，请重新输入");
                            }else {
                                modifyPassword(newPwd);
                            }
                        }
                    },
                    error:function () {

                    }
                });
                return false;
            });

            form.on('submit(cancel)' , function () {
                let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            });

            function modifyPassword(pwd) {
                let jsonData = {
                    pwd:$.fn.blueJs.tools.base64Encode(pwd)
                };
                let type = 'ajaxFormSubmit';
                let msg = $.fn.blueJs.tools.active[type].call(this , '/a/user/modifyPassword' , jsonData);
                //当你在iframe页面关闭自身时
                type = 'saveMsg';
                let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                $.fn.blueJs.tools.active[type].call(this , msg , index);
            }

        });
    </script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="form-horizontal" id="userForm" th:object="${user}" method="post" action="/a/user/modifyPassword">
                        <div class="blue-form-unit">基本信息</div>
                        <input type="hidden" name="id" id="userId" th:field="*{id}"/>

                        <div class="row">
                            <div class="col-xs-6">
                                <div class="form-group">
                                    <label class="control-label col-sm-4">
                                        <span class="blue-required ">*</span> 原密码：<i class="fa icon-question hide"></i>
                                    </label>
                                    <div class="col-sm-8">
                                        <input type="password" class="form-control" id="pwd" value="" placeholder="请输入原登录密码" required>
                                        <span class="blue-required pwd" ></span>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xs-6">
                                <div class="form-group">
                                    <label class="control-label col-sm-4">
                                        <span class="blue-required ">*</span> 新密码：<i class="fa icon-question hide"></i>
                                    </label>
                                    <div class="col-sm-8">
                                        <input type="password" class="form-control" id="newPwd" value="" placeholder="请输入新密码" required>
                                        <span class="blue-required newPwd" ></span>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xs-6">
                                <div class="form-group">
                                    <label class="control-label col-sm-4">
                                        <span class="blue-required ">*</span> 确认密码：<i class="fa icon-question hide"></i>
                                    </label>
                                    <div class="col-sm-8">
                                        <input type="password" class="form-control" id="returnPwd" value="" placeholder="请输入再次输入新密码" required>
                                        <span class="blue-required" ></span>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Form 按钮组-->
                        <div class="layui-form-item layui-layout-admin">
                            <div class="layui-input-block blue-form-button-item">
                                <div class="layui-footer" style="left: 0;">
                                    <button type="button" class="layui-btn" shiro:hasPermission="user:password" lay-submit lay-filter="userSave" onsubmit="return false" id="userSaveBtn" blue-form-id="userForm">立即提交</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                    <button type="button" class="layui-btn layui-btn-primary" onsubmit="return false" id="userCancelBtn">关闭</button>
                                </div>
                            </div>
                        </div>

                    </form>
                </div>
            </div>
        </div>

    </div>
</div>
</body>
</html>